/*******************************************************************************
 * Copyright (c) 2011 Grupo de Sistemas Inteligentes (GSI) - DIT UPM
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *******************************************************************************/
package es.upm.dit.gsi.eclipse.jadex.navigator;

import org.eclipse.core.resources.IFile;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.navigator.IDescriptionProvider;

import es.upm.dit.gsi.eclipse.jadex.util.ImageFactory;

/**
 * Provides a label and icon for objects of type {@link Child}.
 * 
 * @since 3.2
 *
 */
public class AgentLabelProvider extends LabelProvider implements
		ILabelProvider, IDescriptionProvider { 

	public Image getImage(Object element) {
		
		if (element instanceof Child){
			Child data = (Child) element;
			String name = data.getName();
			System.err.println(name);
			if(name.equals("imports")){ //$NON-NLS-1$
				return ImageFactory.getIcon(ImageFactory.PACKAGE); 				
			}
			else if(name.equals("capabilities")){ //$NON-NLS-1$
				return ImageFactory.getIcon(ImageFactory.CAPABILITIES_ICON); 				
			}
			else if(name.equals("beliefs")){ //$NON-NLS-1$
				return ImageFactory.getIcon(ImageFactory.BELIEF_BASE_ICON);
				/*return PlatformUI.getWorkbench().getSharedImages().getImage(
						ISharedImages.IMG_ELCL_COLLAPSEALL);
						*/ 				
			}
			else if(name.equals("plans")){ //$NON-NLS-1$
				return ImageFactory.getIcon(ImageFactory.PLAN_BASE_ICON); 				
			}
			else if(name.equals("goals")){ //$NON-NLS-1$
				return ImageFactory.getIcon(ImageFactory.GOAL_BASE_ICON);		
			}
			else if(name.equals("events")){ //$NON-NLS-1$
				return ImageFactory.getIcon(ImageFactory.EVENTS_ICON);
			}
			else if(name.equals("expressions")){ //$NON-NLS-1$
				return ImageFactory.getIcon(ImageFactory.EXPRESSIONS_ICON); 				
			}
			else if(name.equals("properties")){ //$NON-NLS-1$
				return ImageFactory.getIcon(ImageFactory.SCRIPT); 				
			}
			else if(name.equals("configurations")){ //$NON-NLS-1$
				return ImageFactory.getIcon(ImageFactory.CONFIGURATIONS_ICON);
			}
		}
		else if (element instanceof SubChild){
			SubChild p = (SubChild) element;
			String nodeName = p.getNodeName();
			if(nodeName.equals("plan")){ //$NON-NLS-1$
				return ImageFactory.getIcon(ImageFactory.PLAN_ICON);
			}
			else if(nodeName.equals("achievegoal")){ //$NON-NLS-1$
				return ImageFactory.getIcon(ImageFactory.ACHIEVEGOAL_ICON);
			}
			else if(nodeName.equals("performgoal")){ //$NON-NLS-1$
				return ImageFactory.getIcon(ImageFactory.PERFORMGOAL_ICON);
			}
			else if(nodeName.equals("maintaingoal")){ //$NON-NLS-1$
				return ImageFactory.getIcon(ImageFactory.MAINTAINGOAL_ICON);
			}
			else if(nodeName.equals("querygoal")){ //$NON-NLS-1$
				return ImageFactory.getIcon(ImageFactory.QUERYGOAL_ICON);
			}
			else if(nodeName.equals("metagoal")){ //$NON-NLS-1$
				return ImageFactory.getIcon(ImageFactory.METAGOAL_ICON);
			}
			else if(nodeName.equals("capability")){ //$NON-NLS-1$
				return ImageFactory.getIcon(ImageFactory.BULLET_BLUE);
			}
			else if(nodeName.equals("configuration")){ //$NON-NLS-1$
				return ImageFactory.getIcon(ImageFactory.CONFIGURATION_ENTRY_ICON);
			}
			else if(nodeName.equals("messageevent") || nodeName.equals("internalevent")){ //$NON-NLS-1$
				return ImageFactory.getIcon(ImageFactory.BULLET_ORANGE);				
			}
			else if (nodeName.equals("belief")){ //$NON-NLS-1$
				return ImageFactory.getIcon(ImageFactory.BELIEF_ICON);				
			}
			else if (nodeName.equals("beliefref")){ //$NON-NLS-1$
				return ImageFactory.getIcon(ImageFactory.BELIEF_REF_ICON);
			}
			else if (nodeName.equals("import")){ //$NON-NLS-1$
				return ImageFactory.getIcon(ImageFactory.PACKAGE_GO);
			}
			else if (nodeName.equals("property")){ //$NON-NLS-1$
				return ImageFactory.getIcon(ImageFactory.SCRIPT_GO);
			}
			else if (nodeName.equals("expression")){ //$NON-NLS-1$
				return ImageFactory.getIcon(ImageFactory.BULLET_BLACK);
			}
		}
		else if (element instanceof IFile){
			IFile file = (IFile) element;
			String name = file.getName();
			System.out.println(name);
			boolean endsWith = name.endsWith("agent.xml"); //$NON-NLS-1$
			if (endsWith == true){
				
				return ImageFactory.getIcon(ImageFactory.AGENT_ICON);
			}
		}
		return null;
	}

	public String getText(Object element) {
		String AGENT_EXT = "agent.xml"; //$NON-NLS-1$
		if (element instanceof IFile && ((IFile) element).getName().endsWith(AGENT_EXT)){ 
			String name = ((IFile) element).getName();
			return (name.substring(0, name.indexOf((AGENT_EXT)))); 
		}
		else if (element instanceof Child){
			Child data = (Child) element;
			return data.getName();
		}
		else if (element instanceof SubChild){
			SubChild data = (SubChild) element;
			return data.getHeader() + " : " + data.getDescription(); //$NON-NLS-1$
		}
		return null;
		
	}

	public String getDescription(Object anElement) {
		if (anElement instanceof Child) {
			Child data = (Child) anElement;
			return "Property: " + data.getName(); //$NON-NLS-1$
		}
		return null;
	}
  
}
